package t10086.WorkThread;

import t10086.Data.Pop.PopType;
import t10086.Data.Robot.RobotInfo;
import t10086.Model.Messages.MessageType;
import t10086.WorkThread.Shared.Client.Client;
import t10086.Data.Pop.PopMsg;
import t10086.Model.Messages.Message;
import t10086.WorkThread.Shared.DataBuffer;
import t10086.WorkThread.Shared.ClientMap;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;

public class Listener implements Runnable {
    private final InetSocketAddress socketAddress;
    private final ObjectInputStream in;
    private final LinkedList<Message> buffer;
    private Future<?> future;
    public Listener(InetSocketAddress socketAddress, ObjectInputStream in) {
        this.socketAddress = socketAddress;
        this.in = in;
        this.buffer = new LinkedList<>();
        this.future = CompletableFuture.completedFuture("初始化future");
    }
    @Override
    public void run() {
        System.out.println("Listening to Client " + socketAddress);
        try {
            // 不断读取客户端发送的消息并打印
            Message message;
            while (!Thread.interrupted()) {
                Object msg = in.readObject();
                if (msg == null) continue;
                message = (Message) msg;
                message.setNext(socketAddress);
//                System.out.println(message.getType());
//                System.out.println("Message received: " + message.getType());
//                System.out.println(message);
                buffer.add(message);
//                if(message.getType()== MessageType.ROBOT_UPLOAD){
//                    RobotInfo robotInfo = message.convertData(RobotInfo.class);
//                    System.out.println("Robot["+robotInfo.getSerialNumber()+"] Move TO "+
//                            robotInfo.getPosition()+"NEXT:"+robotInfo.getNext());
//                }
                if(future.isDone()) {
                    LinkedList<Message> clone =  new LinkedList<>(buffer);
                    buffer.clear();
                    this.future = ThreadPoolManager.submitTask(new MsgConvertHandler(clone));
                }
            }
            System.out.println("Listener for " + socketAddress + " has been interrupted");
        } catch (IOException | ClassNotFoundException e) {
           System.out.println(e.getMessage());
        }finally {

            try {
                in.close();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            Client  lostclient = ClientMap.removeClient(socketAddress);
            if (lostclient != null) {
                DataBuffer.getBroadcast().removeClient(lostclient.getSerialNumber());
            }
            PopMsg popMsg = null;
            if (lostclient != null) {
                popMsg = new PopMsg(
                        PopType.LOST,
                        lostclient.getRole(),
                        lostclient.getSerialNumber(),
                        "LOST CONNECTION!"
                );
            }
            DataBuffer.getBroadcast().addPopMsg(popMsg);
        }
    }
}
